/*
 * Copyright (C) 2010, Pino Toscano <pino@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <poppler-document.h>
#include <poppler-image.h>
#include <poppler-page.h>
#include <poppler-page-renderer.h>

#include <cstdlib>
#include <iostream>
#include <memory>
#include <fstream>

#include "parseargs.h"
#include "extractpdf.h"
#if defined(_WIN32) || defined(_WIN64)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
#include <chrono>
#include <string>
#include <iostream>
int fun1() {
    // Create PdfExtractor instance
    PdfExtractor extractor;

    // PDF file path
    std::string fileName = "D:\\PDF\\QQOffline_20240907171241_1.pdf";
    // Set PDF file path
    if (!extractor.setupFilePath(fileName)) {
        std::cerr << "Failed to setup PDF file path" << std::endl;
        return 1;
    }

    // Get page count
    int pageCount = extractor.getPageCount();
    std::cout << "Total pages: " << pageCount << std::endl;

    // Extract text and images page by page
    for (int i = 0; i < pageCount; ++i) {
        bool success = extractor.extractPdfTextAndImagesFromPage(
            i,
            [i](int pageNumber, ContentType type, const std::vector<char>& data, const char* ext) -> bool {
                if (type == ContentType::content_text) {
                    // Print text
                    std::string text(data.data(), data.size());
                    std::cout << "Page " << pageNumber << " Text: " << text << std::endl;
                }
                else if (type == ContentType::content_img) {
                    std::cout << "##### save image:" << std::endl;

                    // Create filename with timestamp
                    auto now = std::chrono::system_clock::now();
                    auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
                        now.time_since_epoch()
                    ).count();

                    std::string fileName = "out/page" + std::to_string(pageNumber) +
                        "_image" + std::to_string(timestamp) +
                        "." + std::string(ext);

                    // Save image data to file

                    std::ofstream outFile(fileName, std::ios::binary);
                    if (outFile) {
                        outFile.write(data.data(), data.size());
                        outFile.close();
                        std::cout << "Saved image: " << fileName << std::endl;
                    }
                    else {
                        std::cerr << "Failed to save image: " << fileName << std::endl;
                    }
                }
                return true; // Continue processing next content
            },
            PdfExtractor::ImageFormat::PNG,
            PdfExtractor::ExtractionMode::TextAndImages
        );

        if (!success) {
            std::cerr << "Failed to extract content from page " << (i + 1) << std::endl;
        }
    }
}
void fun2() {
    std::string fileName = "D:\\PDF\\QQOffline_20240907171241_1.pdf";

    bool success = PdfExtractor::extractAllPdfTextAndImages(
        fileName,
        [](int pageNumber, ContentType type, const std::vector<char>& data, const char* ext) -> bool {
            std::cout << "Page Number: " << pageNumber << std::endl;
            if (type == ContentType::content_text) {
                std::string text(data.begin(), data.end());
                std::cout << "Extracted Text: " << text << std::endl;
            }
            else if (type == ContentType::content_img) {
                // Create filename with timestamp
                auto now = std::chrono::system_clock::now();
                auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
                    now.time_since_epoch()
                ).count();

                std::string imageFileName = "out/image_" + std::to_string(pageNumber) + 
                    "_" + std::to_string(timestamp) + "." + std::string(ext);

                // Save image data to file
                std::ofstream outFile(imageFileName, std::ios::binary);
                if (outFile) {
                    outFile.write(data.data(), data.size());
                    outFile.close();
                    std::cout << "Saved image to " << imageFileName << std::endl;
                }
                else {
                    std::cerr << "Failed to save image " << imageFileName << std::endl;
                }
            }
            return true; // Continue processing next page, return false to stop
        },
        PdfExtractor::ImageFormat::PNG,
        PdfExtractor::ExtractionMode::TextAndImages
    );

    if (success) {
        std::cout << "PDF extraction completed successfully." << std::endl;
    }
    else {
        std::cerr << "PDF extraction failed." << std::endl;
    }
}
int main(int argc, char* argv[])
{
    fun2();

    return 0;
}
